<template>
  <div class="register-container">
    <!-- 注册内容 -->
    <div class="register">
      <h3>
        注册新用户
        <span class="go"
          >我有账号，去 <router-link to="/login">登录</router-link>
        </span>
      </h3>
      <div class="content">
        <label>手机号:</label>
        <input
          type="text"
          placeholder="请输入你的手机号"
          v-model.lazy="phone.number"
          @change="handlerPhoneText"
        />
        <span class="error-msg" v-text="phone.phoneMsg"></span>
      </div>
      <div class="content">
        <label>验证码:</label>
        <input type="text" placeholder="请输入验证码" ref="codeText" /> &nbsp;
        <button style="height: 38px; width: 100px" @click="getCode()">
          获取验证码
        </button>
        <span class="error-msg" v-text="codeMsg"></span>
      </div>
      <div class="content">
        <label>登录密码:</label>
        <input
          type="text"
          placeholder="请输入你的登录密码"
          v-model.lazy="password.pre"
          @change="handlerPrePwd"
        />
        <span class="error-msg" v-text="password.preMsg"></span>
      </div>
      <div class="content">
        <label>确认密码:</label>
        <input
          type="text"
          placeholder="请输入确认密码"
          v-model.lazy="password.next"
          @change="handlerNextPwd"
        />
        <span class="error-msg" v-text="password.nextMsg"></span>
      </div>
      <div class="controls">
        <input name="m1" type="checkbox" :checked="agree" @click="agreeOrNot()"/>
        <span>同意协议并注册《尚品汇用户协议》</span>
        <span class="error-msg" v-text="agreeMsg"></span>
      </div>
      <div class="btn">
        <button @click="handlerRegister">完成注册</button>
      </div>
    </div>

    <!-- 底部 -->
    <div class="copyright">
      <ul>
        <li>关于我们</li>
        <li>联系我们</li>
        <li>联系客服</li>
        <li>商家入驻</li>
        <li>营销中心</li>
        <li>手机尚品汇</li>
        <li>销售联盟</li>
        <li>尚品汇社区</li>
      </ul>
      <div class="address">地址：北京市昌平区宏福科技园综合楼6层</div>
      <div class="beian">京ICP备19006430号</div>
    </div>
  </div>
</template>

<script>
import { regPhone, regPassword } from "@/utils/reg";
import { mapState } from "vuex";
export default {
  name: "Register",
  data() {
    return {
      phone: {
        number: "",
        phoneMsg: "",
      },
      codeMsg: "",
      password: {
        pre: "",
        preMsg: "",
        next: "",
        nextMsg: "",
      },
      agree: true,
      agreeMsg: "",
    };
  },

  computed: {
    ...mapState("user", ["code"]),
  },

  methods: {
    //处理手机号码输入格式
    handlerPhoneText() {
      if (!regPhone.test(this.phone.number)) {
        //不符合格式的情况
        this.phone.number = "";
      }
    },

    async getCode() {
      try {
        if (regPhone.test(this.phone.number)) {
          let phoneNumber = this.phone.number;
          //当输入正确手机格式才执行获取验证码操作
          await this.$store.dispatch("user/getCode", phoneNumber); //派发
          this.$refs.codeText.value = this.code; //输入框赋值
          this.codeMsg = "验证码输入正确！"; //验证码提示
        }
      } catch (error) {
        alert(error.message);
      }
    },

    //处理第一次输入密码
    handlerPrePwd() {
      // 当验证码也不为空时才执行
      if (this.$refs.codeText.value != "") {
        if (regPassword.test(this.password.pre)) {
          //第一次输入密码格式正确执行
          this.password.preMsg = "密码格式正确！";
        } else {
          //第一次输入密码格式错误处理
          this.password.preMsg = "密码格式错误！！！";
        }
      } else {
        //前面步骤并未执行
        alert("请按步骤完成注册");
        this.password.pre = this.password.next = "";
      }
    },

    // 处理第二次输入密码
    handlerNextPwd() {
      if (this.password.pre != "") {
        //前面的步骤已完成的情况
        //判断第一次输入的密码和第二次输入的密码是否相等
        if (this.password.pre == this.password.next) {
          //相等情况
          this.password.nextMsg = "密码输入一致!";
        } else {
          // 不相等情况
          this.password.nextMsg = "两次输入密码不相等!!!";
          this.password.next = "";
        }
      } else {
        //前面步骤位未完成的情况
        alert("请按步骤完成!!!");
        this.password.next = "";
      }
    },

    // 处理注册按钮
    async handlerRegister() {
      // 判断前面步骤是否符合规则
      if (
        regPhone.test(this.phone.number) &&
        this.codeText != "" &&
        regPassword.test(this.password.pre,this.password.next) &&
        this.agree
      ) {
        //符合规则则存储注册信息
        try {
          await this.$store.dispatch("user/getUserRegister", {
            phone: this.phone.number,
            code: this.$refs.codeText.value,
            password: this.password.pre,
          });
          //并且跳转路由
          this.$router.push({
            name: "login",
          });
        } catch (error) {
          alert(error.message);
        }
      }else{

      }
    },

    // 协议按钮处理
    agreeOrNot(){
      this.agree = !this.agree;
      console.log(this.agree);
    }
  },

  watch: {
    phone: {
      deep: true,
      handler() {
        this.$nextTick(() => {
          if (!regPhone.test(this.phone.number)) {
            //若手机格式有误，提示框将发生变化
            this.phone.phoneMsg = "格式错误！！！";
          } else {
            this.phone.phoneMsg = "格式正确！！！";
          }
        });
      },
    },
  },
};
</script>

<style lang="less" scoped>
.register-container {
  .register {
    width: 1200px;
    height: 445px;
    border: 1px solid rgb(223, 223, 223);
    margin: 0 auto;

    h3 {
      background: #ececec;
      margin: 0;
      padding: 6px 15px;
      color: #333;
      border-bottom: 1px solid #dfdfdf;
      font-size: 20.04px;
      line-height: 30.06px;

      span {
        font-size: 14px;
        float: right;

        a {
          color: #e1251b;
        }
      }
    }

    div:nth-of-type(1) {
      margin-top: 40px;
    }

    .content {
      padding-left: 390px;
      margin-bottom: 18px;
      position: relative;

      label {
        font-size: 14px;
        width: 96px;
        text-align: right;
        display: inline-block;
      }

      input {
        width: 270px;
        height: 38px;
        padding-left: 8px;
        box-sizing: border-box;
        margin-left: 5px;
        outline: none;
        border: 1px solid #999;
      }

      img {
        vertical-align: sub;
      }

      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        color: red;
      }
    }

    .controls {
      text-align: center;
      position: relative;

      input {
        vertical-align: middle;
      }

      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        color: red;
      }
    }

    .btn {
      text-align: center;
      line-height: 36px;
      margin: 17px 0 0 55px;

      button {
        outline: none;
        width: 270px;
        height: 36px;
        background: #e1251b;
        color: #fff !important;
        display: inline-block;
        font-size: 16px;
      }
    }
  }

  .copyright {
    width: 1200px;
    margin: 0 auto;
    text-align: center;
    line-height: 24px;

    ul {
      li {
        display: inline-block;
        border-right: 1px solid #e4e4e4;
        padding: 0 20px;
        margin: 15px 0;
      }
    }
  }
}
</style>